package cn.ven.controller;

import cn.ven.entity.*;
import cn.ven.service.*;
import cn.ven.tool.RespJson;
import cn.ven.tool.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/fore")
public class ForeController {

    private Logger logger =LoggerFactory.getLogger(ForeController.class);
    @Autowired
    BlogService blogService;
    @Autowired
    TypeService typeService;
    @Autowired
    ContentService contentService;
    @Autowired
    UserService userService;
    @Autowired
    SupportService supportService;


    //根据id查找用户
    @RequestMapping("/user/findByUid/{id}")
    @ResponseBody
    public Map<String,Object> findByUid(@PathVariable("id")String id){
        return RespJson.pack(200,"",userService.findById(id));
    }

    //查找所有的用户
    @RequestMapping("/user/findAllUser")
    @ResponseBody
    public Map<String,Object> findAllUser(){
        return RespJson.pack(200,"",userService.findAll());
    }

    //跳转到博客的页面
    @RequestMapping("/blog/go2Blog")
    public String go2Blog(){
        return "blog/blog";
    }

    //写博客
    //前端直接获取到类型id直接set进blog对象里面
    //两表添加博客 t_blog t_content
    //t_content添加完内容以后返回contentId 跟t_blog contentId绑定
    //用户uid直接在服务层里面利用工具类SessionUtil获取
    @RequestMapping("/blog/addBlog")
    @ResponseBody
    public Map<String,Object> addBlog(Content content, Blog blog){
        logger.debug("blog==="+blog);
        logger.debug("content==="+content);
        String contentId = contentService.addContent(content);
        logger.debug("contentId===="+contentId);
        blog.setContentId(contentId);
        blogService.addBlog(blog);
        return RespJson.pack(200,"添加成功","");
    }


    //真删除
    @RequestMapping("/blog/delBlog/{id}")
    @ResponseBody
    public Map<String,Object> delBlog(@PathVariable("id")String id){
        Blog blog = blogService.findById(id);//根据id查找对应博客
        if (blog.getUid().equals(SessionUtil.getUser().getId())){ //当bokeUid跟当前用户id如果一致，就是证明是当前用户
            blogService.delBlog(id);//删除博客
            contentService.delContent(blog.getContentId());//博客内容对应也要删除
            supportService.delSupportByBlogid(id); //跟博客id绑定点赞的都要删除
            return RespJson.pack(200,"删除成功","");
        }
        return RespJson.pack(403,"没有权限","");
    }


    //根据id查找对应博客信息
    @RequestMapping("/blog/findBlogTypeContent/{id}")
    @ResponseBody
    public Map<String,Object> findBlogTypeContent(@PathVariable("id")String id){
        BlogTypeContent blogTypeContent = blogService.findBlogTypeContent(id);
        logger.debug("blogTypeContent====="+blogTypeContent);
        return RespJson.pack(200,"",blogTypeContent);
    }

    //查找所有的博客
    //当isdel==1时，就是没删除
    @RequestMapping("/blog/findAllBlog")
    @ResponseBody
    public  Map<String,Object> findAllBlog(){
        return RespJson.pack(200,"",blogService.findAll(1));
    }


    //点赞加一
    @RequestMapping("/blog/addThmub_num/{id}")
    @ResponseBody
    public Map<String,Object> addThmub_num(@PathVariable("id")String id){
        MyUser user = SessionUtil.getUser();//获取当前用户
        Support oneSupport = supportService.findOneSupport(user.getId(), id);
        if (oneSupport!=null){
                if (oneSupport.getUid().equals(user.getId())&&oneSupport.getBlogid().equals(id)){  //如果uid和博客id都同时存在，标记已经点赞过了
                    return RespJson.pack(500,"点赞失败，你已经点赞过了","");
                }
        }
        Support support =new Support();//new一个support对象
        support.setBlogid(id);
        support.setUid(user.getId());
        System.out.println("support==="+support);
        supportService.add(support);//同时往里面添加点赞对象，证明以后点过赞了
        blogService.updateThmub_mum(id); //点赞加1
        List<Blog> blogs = blogService.findAll(1);
        return RespJson.pack(200,"点赞成功",blogs);
    }

    //取消点赞
    @RequestMapping("/blog/reduceThmub_num/{id}")
    @ResponseBody
    public Map<String,Object> reduceThmub_num(@PathVariable("id")String id){
        MyUser user = SessionUtil.getUser();//获取当前用户
        Support oneSupport = supportService.findOneSupport(user.getId(), id);
        if (oneSupport!=null){
            if (oneSupport.getUid().equals(user.getId())&&oneSupport.getBlogid().equals(id)){  //如果uid和博客id都同时存在，标记已经点赞过了
                supportService.delSupport(user.getId(),id);
                blogService.reduceThmub_mum(id);
                List<Blog> blogs = blogService.findAll(1);
                return RespJson.pack(200,"取消点赞成功",blogs);
            }
        }
        List<Blog> blogs = blogService.findAll(1);
        return RespJson.pack(500,"你没有点赞",blogs);
    }


    //假删除
    @RequestMapping("/blog/falseDel/{id}")
    @ResponseBody
    public Map<String,Object> falseDel(@PathVariable("id")String id){
        blogService.falseDel(id);
        List<Blog> blogs = blogService.findAll(1);
        return RespJson.pack(200,"",blogs);
    }

    //用户查看自己被假删除的帖子
    @RequestMapping("/blog/findMyDelBlogs")
    @ResponseBody
    public Map<String,Object> findMyDelBlogs(){
        List<Blog> blogs = blogService.findMyDelBlogs();
        List<BlogContent> blogContents =new ArrayList<>();
        for (int i=0;i<blogs.size();i++){
            BlogContent blogContent =new BlogContent();
            blogContent.setBlog(blogs.get(i));
            Content content = contentService.findById(blogs.get(i).getContentId());
            blogContent.setContent(content);
            blogContents.add(blogContent);
        }
        return RespJson.pack(200,"",blogContents);
    }



    //查找所有的类型
    @RequestMapping("/type/findAllType")
    @ResponseBody
    public Map<String,Object> findAllType(){
        List<Type> types = typeService.findAll();
        logger.debug("types==="+types);
        return RespJson.pack(200,"查找成功",types);
    }


    //根据id查找类型
    @RequestMapping("/type/findTypeById/{id}")
    @ResponseBody
    public Map<String,Object> findTypeById(@PathVariable("id")String id){
        Type type = typeService.findById(id);
        logger.debug("type==="+type);
        return RespJson.pack(200,"",type);
    }

    //添加类型
    @RequestMapping("/type/addType")
    @ResponseBody
    public Map<String,Object>addType(@RequestBody Type type){
        Type type1 = typeService.findTypeByType(type.getType());
        if (type1==null){ //当类型不存在
            typeService.addType(type);
            List<Type> types = typeService.findAll();
            return RespJson.pack(200,"添加成功",types);
        }
        List<Type> types = typeService.findAll();
        return RespJson.pack(500,"该类型已经存在，无法添加",types);
    }


    //跳转到个人中心页面
    @RequestMapping("/person/go2PersonCenter")
    public String go2PersonCenter(){
        return "person/person";
    }

    //查看自己博客
    @RequestMapping("/person/findMyBlog")
    @ResponseBody
    public Map<String,Object> findMyBlog(){
        List<Blog> blogs = blogService.findByUid(SessionUtil.getUser().getId());
        List<BlogContent> lst =new ArrayList<>();
        for (int i=0;i<blogs.size();i++){
            BlogContent blogContent =new BlogContent();
            if (blogs.get(i).getIsdel()==1){
                blogContent.setBlog(blogs.get(i));
                Content content = contentService.findById(blogs.get(i).getContentId());
                blogContent.setContent(content);
                lst.add(blogContent);
            }

        }
        return RespJson.pack(200,"",lst);
    }

    //恢复被删除的博客
    @RequestMapping("/blog/recoverBlog/{id}")
    @ResponseBody
    public Map<String,Object> recoverBlog(@PathVariable("id")String id){
        blogService.recoverBlog(id);
        List<Blog> blogs = blogService.findAll(0);
        List<BlogContent> lst =new ArrayList<>();
        for (int i=0;i<blogs.size();i++){
                BlogContent blogContent =new BlogContent();
                blogContent.setBlog(blogs.get(i));
                Content content = contentService.findById(blogs.get(i).getContentId());
                blogContent.setContent(content);
                lst.add(blogContent);
            }
        return RespJson.pack(200,"",lst);
    }




}
